home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 301-325 / disk_313 / uucp / uucp1.lzh / src / GUtil / uuserdump.c < prev   
C/C++ Source or Header  |  1990-01-10  |  4KB  |  151 lines

  1.  
  2. /*
  3.  *  UUSERDUMP.C
  4.  *
  5.  *  (C) Copyright 1989-1990 by Matthew Dillon,  All Rights Reserved.
  6.  *
  7.  *  UUSERDUMP filename
  8.  *
  9.  *  KEY THINGS ABOUT UUSER: USED AS STDIN/STDOUT
  10.  *
  11.  *    Currently UUSER: works only with single sessions started
  12.  *    by Getty (this has to do with how UUSER: deals with carrier
  13.  *    detect).  THIS WILL BE FIXED.
  14.  *
  15.  *    (1) Use Level 1 I/O if you can (read/write)
  16.  *        read() returns 0 on timeout, -1 on carrier lost
  17.  *        otherwise, read() returns the immediate number of
  18.  *        data bytes ready up to the amount you requested.
  19.  *        (i.e. if you read(0,buf,256) you can get anywhere from
  20.  *        -1, 0, 1 to 256 back).
  21.  *
  22.  *        write() returns the number you wrote or -1 (lost carrier)
  23.  *
  24.  *        To 'poll' data ready you can read(0, NULL, 0) .. reading
  25.  *        0 bytes returns 0 (data ready) or -1 (data not ready).
  26.  *        NOTE: 0 (data ready) will be returned if carrier is lost
  27.  *        when you read 0 bytes... this is so your program thinks
  28.  *        data is ready and when it does a real read it finds that
  29.  *        carrier was lost!
  30.  *
  31.  *    (2) If you want to use Level 2 I/O (stdio) I suggest you use
  32.  *        it for writing only.  If you really want to use it for
  33.  *        reading remember that the timeout will cause an EOF
  34.  *        condition which must be cleared (clrerr()).  And you
  35.  *        must call ferror() to determine whether an EOF is an
  36.  *        EOF (timeout()) or an actual error (lost carrier).
  37.  */
  38.  
  39. #include <proto/all.h>
  40. #include <stdio.h>
  41. #include "/version.h"
  42.  
  43. IDENT(".00");
  44.  
  45. void
  46. CXBRK()             /*  on BREAK    */
  47. {
  48.     exit(1);
  49. }
  50.  
  51. char buf[256];
  52. short i;
  53.  
  54. void
  55. main(ac, av)
  56. char *av[];
  57. {
  58.     short i;
  59.     char c;
  60.     short n;
  61.     FILE *fi = fopen(av[1], "r");
  62.  
  63.     /*
  64.      *    set output to line buffering (stdio), else it will use
  65.      *    file buffering!  Which is bad if we want to catch ^S/^C
  66.      */
  67.  
  68.     setvbuf(stdout, NULL, _IOLBF, 0);
  69.  
  70.  
  71.     if (ac == 1)
  72.     exit(1);
  73.  
  74.     if (fi == NULL)
  75.     printf("Info file %s not available\r\n", av[1]);
  76.  
  77.     printf("Dumping info file (suggest capture), hit Enter when ready\n");
  78.     fflush(stdout);
  79.  
  80.     /*
  81.      *    Getty uses the R1000 option, so there is a one second timeout
  82.      *    on reads when no data is present.  If data is present, UUSER:
  83.      *    returns the number of bytes immediately available regardless of
  84.      *    how many you requested.
  85.      */
  86.  
  87.     for (i = 0; i < 60; ++i) {
  88.     n = read(0, &c, 1);
  89.     if (n < 0)              /*  lost carrier */
  90.         exit(1);
  91.                 /*  success    */
  92.     if (n > 0 && (c == 10 || c == 13))
  93.         goto skip;
  94.     /* timeout */
  95.     }
  96.     exit(1);
  97. skip:
  98.     while (fgets(buf, 256, fi)) {
  99.     if (CheckControl())
  100.         break;
  101.     fputs(buf, stdout);
  102.     }
  103.     fclose(fi);
  104.  
  105.     printf("End of dump, any key to disconnect\n");
  106.     fflush(stdout);
  107.     for (i = 0; i < 60; ++i) {
  108.     n = read(0, &c, 1);
  109.     if (n == 0)     /*  1 second timeout    */
  110.         continue;
  111.     break;        /*  any key or lost cd    */
  112.     }
  113. }
  114.  
  115. /*
  116.  *  Here we use a UUSER: trick ... requesting 0 bytes in a read()
  117.  *  incurs NO timeout.    0 is returned if data is ready, -1 if data
  118.  *  is not ready.
  119.  *
  120.  *  Another way to do it is to dump a seconds worth of data, then
  121.  *  call read() (which blocks for up to 1 second if no data is
  122.  *  ready, but the user doesn't see that because we've already queued
  123.  *  a second's worth of data).  The first method is better because
  124.  *  there is finer granularity (that is, the one used below)
  125.  */
  126.  
  127. CheckControl()
  128. {
  129.     char c = 0;
  130.     short n;
  131.  
  132.     if (read(0, NULL, 0) == 0) {    /*  returns 0=data ready, -1=not rdy */
  133.     read(0, &c, 1);
  134.     if (c == ('s'&0x1F)) {      /*  ^S           */
  135.         for (;;) {
  136.         n = read(0, &c, 1);
  137.         if (n < 0)          /*  lost carrier */
  138.             exit(1);
  139.         if (n == 0)         /*  timeout      */
  140.             continue;
  141.         break;            /*    got a key    */
  142.         }
  143.         return(0);
  144.     }
  145.     if (c == ('c'&0x1F))        /*  ^C           */
  146.         return(1);
  147.     }
  148.     return(0);
  149. }
  150.  
  151.